var e = require;
var t = module;
var n = exports;
var p =
    (this && this.__assign) ||
    function() {
        return (p =
            Object.assign ||
            function(e) {
                for (var t, n = 1, o = arguments.length; n < o; n++)
                    for (var i in (t = arguments[n])) Object.prototype.hasOwnProperty.call(t, i) && (e[i] = t[i]);
                return e;
            }).apply(this, arguments);
    },
    o =
    (this && this.__awaiter) ||
    function(e, a, s, c) {
        return new(s = s || Promise)(function(n, t) {
            function o(e) {
                try {
                    r(c.next(e));
                } catch (e) {
                    t(e);
                }
            }

            function i(e) {
                try {
                    r(c.throw(e));
                } catch (e) {
                    t(e);
                }
            }

            function r(e) {
                var t;
                e.done ?
                    n(e.value) :
                    ((t = e.value) instanceof s ?
                        t :
                        new s(function(e) {
                            e(t);
                        })
                    ).then(o, i);
            }
            r((c = c.apply(e, a || [])).next());
        });
    },
    h =
    (this && this.__generator) ||
    function(n, o) {
        var i,
            r,
            a,
            s = {
                label: 0,
                sent: function() {
                    if (1 & a[0]) throw a[1];
                    return a[1];
                },
                trys: [],
                ops: []
            },
            e = { next: t(0), throw: t(1), return: t(2) };
        return (
            "function" == typeof Symbol &&
            (e[Symbol.iterator] = function() {
                return this;
            }),
            e
        );

        function t(t) {
            return function(e) {
                return (function(t) {
                    if (i) throw new TypeError("Generator is already executing.");
                    for (; s;)
                        try {
                            if (
                                ((i = 1),
                                    r &&
                                    (a =
                                        2 & t[0] ?
                                        r.return :
                                        t[0] ?
                                        r.throw || ((a = r.return) && a.call(r), 0) :
                                        r.next) &&
                                    !(a = a.call(r, t[1])).done)
                            )
                                return a;
                            switch (((r = 0), (t = a ? [2 & t[0], a.value] : t)[0])) {
                                case 0:
                                case 1:
                                    a = t;
                                    break;
                                case 4:
                                    return s.label++, { value: t[1], done: !1 };
                                case 5:
                                    s.label++, (r = t[1]), (t = [0]);
                                    continue;
                                case 7:
                                    (t = s.ops.pop()), s.trys.pop();
                                    continue;
                                default:
                                    if (!(a = 0 < (a = s.trys).length && a[a.length - 1]) &&
                                        (6 === t[0] || 2 === t[0])
                                    ) {
                                        s = 0;
                                        continue;
                                    }
                                    if (3 === t[0] && (!a || (t[1] > a[0] && t[1] < a[3]))) {
                                        s.label = t[1];
                                        break;
                                    }
                                    if (6 === t[0] && s.label < a[1]) {
                                        (s.label = a[1]), (a = t);
                                        break;
                                    }
                                    if (a && s.label < a[2]) {
                                        (s.label = a[2]), s.ops.push(t);
                                        break;
                                    }
                                    a[2] && s.ops.pop(), s.trys.pop();
                                    continue;
                            }
                            t = o.call(n, s);
                        } catch (e) {
                            (t = [6, e]), (r = 0);
                        } finally {
                            i = a = 0;
                        }
                    if (5 & t[0]) throw t[1];
                    return { value: t[0] ? t[1] : void 0, done: !0 };
                })([t, e]);
            };
        }
    };
Object.defineProperty(n, "__esModule", { value: !0 }), (n.WeChatPlatform = void 0);
var d = e("index"),
    i = e("common"),
    f = e("const"),
    r = e("env"),
    c = e("events"),
    l = e("user-data"),
    u = e("platform-const"),
    m = e("wechat-ad"),
    e =
    ((a.prototype.string = function() {
            return u.PlatformCode.Wechat;
        }),
        (a.prototype.initSDK = function() {
            this.initWechatListener(), this.initCommonSetting(), this.initTga();
        }),
        (a.prototype.initTga = function() {
            var e;
            r.env.mode === r.EnvMode.PROD &&
                ((e = new ThinkingAnalyticsAPI(u.TgaConfig)).setSuperProperties({
                        ver: r.env.version,
                        platfrom: this.string(),
                        game: r.env.game
                    }),
                    e.init(),
                    l.userData.setUid(e.getDistinctId()),
                    (this.ta = e));
        }),
        (a.prototype.isSwitchedInitd = function() {
            return this.switchesInitd;
        }),
        (a.prototype.initWechatListener = function() {
            var n = this;
            wx.onShow(function(e) {
                    var t = Date.now();
                    console.log(u.LogTag, "on show", t),
                        c.platformEvent.emit({ onShow: !0, isSharing: n.isSharing }),
                        l.userData.syncLaunchOption(e),
                        n.isSharing || n.reenter();
                }),
                wx.onHide(function() {
                    (n.onHideT = Date.now()),
                    console.log(u.LogTag, "on hide", n.onHideT),
                        c.platformEvent.emit({ onHide: !0 });
                }),
                wx.onError(function(e) {
                    r.env.mode !== r.EnvMode.PROD &&
                        wx.showModal({
                            title: e.message || "异常报错",
                            content: e.stack || e + "",
                            showCancel: !1,
                            confirmText: "好的"
                        });
                }),
                wx.onShareAppMessage(function() {
                    return u.SDKConfig.shareData;
                }),
                wx.showShareMenu({ withShareTicket: !0 }),
                this.canUse("2.0.2") &&
                wx.onMemoryWarning(function() {
                    wx.triggerGC();
                });
            var t = wx.getUpdateManager();
            t.onCheckForUpdate(function(e) {
                    console.log(u.LogTag, "update checked, hasUpdate=" + e.hasUpdate);
                }),
                t.onUpdateFailed(function() {
                    console.log(u.LogTag, "update Failed");
                }),
                t.onUpdateReady(function() {
                    wx.showModal({
                        title: "更新提示",
                        content: "新版本已经准备好，是否重启应用？",
                        success: function(e) {
                            e.confirm && t.applyUpdate();
                        }
                    });
                });
        }),
        (a.prototype.initCommonSetting = function() {
            var t = this;
            l.userData.syncLaunchOption(wx.getLaunchOptionsSync()),
                wx.getNetworkType().then(function(e) {
                    l.userData.network = e;
                }),
                c.cheatEvent.on(function(e) {
                    "noAd" == e
                        ?
                        ((t.noAd = !t.noAd), d.showToast("分享视频直接成功" + t.noAd)) :
                        "switchVideo" == e ?
                        ((t.noVideo = !t.noVideo), d.showToast("强制没有视频" + t.noVideo)) :
                        "clearStorage" == e && t.clearStorage();
                });
        }),
        (a.prototype.setAccountID = function() {
            r.env.mode, r.EnvMode.TEST;
        }),
        (a.prototype.login = function() {
            return o(this, void 0, Promise, function() {
                return h(this, function() {
                    return [2, null];
                });
            });
        }),
        (a.prototype.reenter = function() {}),
        (a.prototype.getStorage = function(t) {
            try {
                return JSON.parse(wx.getStorageSync(t));
            } catch (e) {
                t = "";
                null != e && null != e.message && (t = e.message),
                    this.trackEvent("error", { type: "getStorage", message: t });
            }
        }),
        (a.prototype.setStorage = function(e, t) {
            try {
                wx.setStorageSync(e, JSON.stringify(t));
            } catch (e) {
                t = "";
                null != e && null != e.message && (t = e.message),
                    this.trackEvent("error", { type: "setStorage", message: t });
            }
        }),
        (a.prototype.clearStorage = function() {
            try {
                wx.clearStorageSync();
            } catch (e) {
                var t = "";
                null != e && null != e.message && (t = e.message),
                    this.trackEvent("error", { type: "cleanStorage", message: t });
            }
        }),
        (a.prototype.removeStorage = function(t) {
            try {
                wx.removeStorageSync(t);
            } catch (e) {
                t = "";
                null != e && null != e.message && (t = e.message),
                    this.trackEvent("error", { type: "removeStorage", message: t });
            }
        }),
        (a.prototype.trackLogin = function() {
            throw new Error("Method not implemented.");
        }),
        (a.prototype.trackUserSet = function(e) {
            null != this.ta && this.ta.userSet(e);
        }),
        (a.prototype.trackUserSetOnce = function(e) {
            null != e && null != this.ta && this.ta.userSetOnce(e);
        }),
        (a.prototype.trackEvent = function(e, t) {
            null != this.ta &&
                ((t = p({ runTime: l.userData.enterAt ? (Date.now() - l.userData.enterAt) / 1e3 : 0 }, t)),
                    this.ta.track(e, t));
        }),
        (a.prototype.getSeq = function() {
            throw new Error("Method not implemented.");
        }),
        (a.prototype.consumeSeq = function(t) {
            return o(this, void 0, Promise, function() {
                return h(this, function(e) {
                    switch (e.label) {
                        case 0:
                            return (
                                console.log(u.LogTag, "consume sequence", t),
                                t.sequenceInfo.type == u.STDSeqType.VIDEO ? [4, this.doWatchAD(t)] : [3, 2]
                            );
                        case 1:
                            return e.sent(), [3, 4];
                        case 2:
                            return [4, this.doShare(t)];
                        case 3:
                            e.sent(), (e.label = 4);
                        case 4:
                            return [2];
                    }
                });
            });
        }),
        (a.prototype.doWatchAD = function(s) {
            var e;
            return o(this, void 0, Promise, function() {
                var i, r, a;
                return h(this, function() {
                    return this.noAd ? [2] :
                        this.noVideo ? [2, Promise.reject(u.VideoFailCode.NO_AD)] :
                        (c.adEvent.emit(f.AdEvent.VIDEO),
                            (i = m.VideoUnit[null !== (e = s.videoDuration) && void 0 !== e ? e : 30]), !this.preloadVideo(i) || null == (r = this.videoMap[i]) ?
                            (c.adEvent.emit(f.AdEvent.VIDEO_END), [2, Promise.reject(u.VideoFailCode.NO_AD)]) :
                            (this.showLoading(),
                                (a = this), [
                                    2,
                                    new Promise(function(n, o) {
                                        r.onClose(function e(t) {
                                                null != r && r.offClose(e),
                                                    null != t && t.isEnded ?
                                                    (c.adEvent.emit(f.AdEvent.VIDEO_END),
                                                        a.trackEvent("video", {
                                                            step: "complete",
                                                            unit: i,
                                                            type: s.videoSource,
                                                            item: s.item
                                                        }),
                                                        l.userData.incVideoCnt(),
                                                        n()) :
                                                    (c.adEvent.emit(f.AdEvent.VIDEO_END),
                                                        a.trackEvent("video", {
                                                            step: "cancel",
                                                            unit: i,
                                                            type: s.videoSource,
                                                            item: s.item
                                                        }),
                                                        o(u.VideoFailCode.CANCELED));
                                            }),
                                            r
                                            .show()
                                            .then(function() {
                                                a.trackEvent("video", {
                                                    step: "display",
                                                    success: !0,
                                                    unit: i,
                                                    type: s.videoSource,
                                                    item: s.item
                                                });
                                            })
                                            .catch(function(e) {
                                                console.log("<<<< video err: ", e),
                                                    a.trackEvent("video", {
                                                        step: "display",
                                                        success: !1,
                                                        type: s.videoSource,
                                                        item: s.item
                                                    }),
                                                    c.adEvent.emit(f.AdEvent.VIDEO_END),
                                                    r.destroy(),
                                                    (a.videoMap[i] = null),
                                                    o(u.VideoFailCode.NO_AD);
                                            })
                                            .then(function() {
                                                a.hideLoading();
                                            });
                                    })
                                ]));
                });
            });
        }),
        (a.prototype.doShare = function() {
            return o(this, void 0, Promise, function() {
                return h(this, function() {
                    throw new Error("Method not implemented.");
                });
            });
        }),
        (a.prototype._askReShare = function() {
            return o(this, void 0, Promise, function() {
                return h(this, function() {
                    return [
                        2,
                        new Promise(function(t) {
                            wx.showModal({
                                title: "提示",
                                content: "分享失败",
                                showCancel: !0,
                                cancelText: "取消",
                                confirmText: "去分享",
                                confirmColor: "#576B95",
                                success: function(e) {
                                    e.confirm ? t(!0) : e.cancel && t(!1);
                                },
                                fail: function() {
                                    t(!1);
                                }
                            });
                        })
                    ];
                });
            });
        }),
        (a.prototype.canUse = function(e) {
            return 0 <= i.compareVersion(this.getSystemData().SDKVersion, e);
        }),
        (a.prototype.getSystemData = function() {
            if (null == this.systemInfoCache)
                try {
                    this.systemInfoCache = wx.getSystemInfoSync();
                } catch (e) {
                    return u.DefaultSystemInfo;
                }
            return this.systemInfoCache;
        }),
        (a.prototype.getSwitches = function() {
            return this.sdkSwitch;
        }),
        (a.prototype.vibrate = function(e) {
            e == u.VibrateType.SHORT ?
                wx.vibrateShort({ type: "medium" }) :
                e == u.VibrateType.LONG && wx.vibrateLong({ success: function() {}, fail: function() {} });
        }),
        (a.prototype.preloadVideo = function(t) {
            if (!this.canUse("2.0.4")) return !1;
            var e = this.videoCheckedMap[t];
            if (e) return e;
            var n,
                o,
                i = this.videoCheckedLastT[t],
                r = Date.now();
            return (!e &&
                (null == i || r - i >= u.SDKConfig.videoCoolTime) &&
                (null == (o = this.videoMap[t]) &&
                    ((n = this),
                        (o = wx.createRewardedVideoAd({ adUnitId: t, multiton: !0 })),
                        (this.videoMap[t] = o).onLoad(function() {
                            console.log("<<<< preload video success"),
                                (n.videoCheckedMap[t] = !0),
                                (n.videoCheckedLastT[t] = Date.now()),
                                n.trackEvent("video", { step: "pull", success: !0, unit: t });
                        }),
                        o.onError(function(e) {
                            console.log("<<<< preload video err: ", e),
                                (n.videoMap[t] = null),
                                (n.videoCheckedMap[t] = !1),
                                (n.videoCheckedLastT[t] = Date.now()),
                                o.destroy(),
                                n.trackEvent("video", { step: "pull", success: !1, unit: t });
                        })),
                    null != o && o.load()), !1
            );
        }),
        (a.prototype.showModal = function(e) {
            return new Promise(function(t) {
                wx.showModal(
                    p(p({}, e), {
                        success: function(e) {
                            e.confirm ? t(!0) : e.cancel && t(!1);
                        },
                        fail: function() {
                            t(!1);
                        }
                    })
                );
            });
        }),
        (a.prototype.refreshGame = function() {
            wx.exitMiniProgram();
        }),
        (a.prototype.showLoading = function() {
            this.canUse("1.1.0") && ((this.isLoading = !0), wx.showLoading({ title: "加载中", mask: !0 }));
        }),
        (a.prototype.hideLoading = function() {
            this.canUse("1.1.0") && this.isLoading && (wx.hideLoading(), (this.isLoading = !1));
        }),
        (a.prototype.log = function(e) {
            for (var t = [], n = 1; n < arguments.length; n++) t[n - 1] = arguments[n];
        }),
        (a.prototype.getMenuBounding = function() {
            var e, t, n, o;
            return (
                null == this.menuBounding &&
                (this.canUse("2.1.0") ?
                    ((e = wx.getMenuButtonBoundingClientRect()),
                        (o = this.getSystemData()),
                        (n = (t = cc.view.getFrameSize()).width / o.screenWidth),
                        (o = t.height / o.screenHeight),
                        (this.menuBounding = cc.rect(e.left * n, e.top * o, e.width * n, e.height * o))) :
                    (this.menuBounding = new cc.Rect(650, 0, 100, 50))),
                this.menuBounding
            );
        }),
        (a.prototype.createShareImage = function(l, u) {
            return o(this, void 0, Promise, function() {
                var n,
                    o,
                    i,
                    r,
                    a,
                    s,
                    c = this;
                return h(this, function() {
                    return f.ShareImage.has(l) ?
                        ((this.cacheCanvas = wx.createCanvas()),
                            this.cacheCanvas,
                            (this.cacheCanvas = wx.createCanvas()),
                            ((n = this.cacheCanvas).width = 400),
                            (n.height = 320),
                            (o = n.width),
                            (r = 320 * (i = o / 400)),
                            (a = n.getContext("2d")).clearRect(0, 0, o, r),
                            ((s = wx.createImage()).src = f.ShareImage.get(l)), [
                                2,
                                new Promise(function(t) {
                                    (s.onload = function() {
                                        var e;
                                        a.clearRect(0, 0, o, r),
                                            a.drawImage(s, 0, 0, o, r),
                                            c.drawImage(l, a, p({ scale: i, aimWidth: o }, u)) ?
                                            ((e = n.toTempFilePathSync({
                                                    x: 0,
                                                    y: 0,
                                                    width: o,
                                                    height: r,
                                                    destWidth: o,
                                                    destHeight: r
                                                })),
                                                t(e)) :
                                            t(null);
                                    }),
                                    (s.onerror = function(e) {
                                        console.error(e), d.showToast("网络错误~", 1500), t(null);
                                    });
                                })
                            ]) : [2, null];
                });
            });
        }),
        (a.prototype.drawImage = function() {
            return !1;
        }),
        (a.prototype.preloadInterstitialAD = function(t) {
            if (((t = m.getInterstitialAdUnit()), l.userData.isBannerAdUser())) {
                var n = this;
                if (n.canUse("2.6.0"))
                    try {
                        (n.interstitialAd = wx.createInterstitialAd({ adUnitId: t })),
                        (n.interAdReady = !1),
                        n.interstitialAd.onLoad(function() {
                                n.trackEvent("interstitial", { step: "pull", unit: t, bool: !0 }), (n.interAdReady = !0);
                            }),
                            n.interstitialAd.onError(function(e) {
                                n.trackEvent("interstitial", {
                                        step: "pull",
                                        unit: t,
                                        bool: !1,
                                        message: e.errMsg || ""
                                    }),
                                    (n.interAdReady = !1),
                                    n.interstitialAd.destroy(),
                                    (n.interstitialAd = null);
                            }),
                            n.interstitialAd.load(),
                            n.interstitialAd.onClose(function() {
                                n.interstitialAd = null;
                            });
                    } catch (e) {
                        console.error(u.LogTag, e),
                            (n.interAdReady = !1),
                            n.interstitialAd.destroy(),
                            (n.interstitialAd = null);
                    }
            }
        }),
        (a.prototype.showInterstitialAD = function() {
            return o(this, void 0, Promise, function() {
                var e;
                return h(this, function() {
                    return (
                        null == this.interstitialAd ||
                        (this.interAdReady &&
                            (e = this).interstitialAd
                            .show()
                            .then(function() {
                                e.trackEvent("interstitial", { step: "display", bool: !1 });
                            })
                            .catch(function() {
                                e.trackEvent("interstitial", { step: "display", bool: !1 });
                            })), [2]
                    );
                });
            });
        }),
        (a.prototype.canShowBanner = function() {
            return null != this.m_bInitBanner ?
                this.m_bInitBanner :
                !l.userData.isBannerAdUser() || this.getBannerSize().height <= 0 ?
                (this.m_bInitBanner = !1) :
                (this.m_bInitBanner = !0);
        }),
        (a.prototype.getBannerSize = function() {
            var e = new cc.Size(0, 0),
                t = this.getSystemData().windowWidth,
                n = this.getSystemData().windowHeight;
            if (t < 300) return e;
            var o = t,
                t = 0.35 * o;
            return (
                (o =
                    (n - t) / n < cc.view.getDesignResolutionSize().height / cc.view.getVisibleSize().height ?
                    (300 / 105) *
                    (t = n - (cc.view.getDesignResolutionSize().height / cc.view.getVisibleSize().height) * n) :
                    o) < 300 ||
                (600 <= o && (t = 0.35 * (o = 600)), (e.width = Math.ceil(o)), (e.height = Math.ceil(t))),
                e
            );
        }),
        (a.prototype.showBanner = function() {
            var e = this.getSystemData().windowWidth,
                t = this.getSystemData().windowHeight,
                n = this.getBannerSize(),
                t = t - n.height,
                e = (e - n.width) / 2,
                n = wx.createBannerAd({
                    adUnitId: m.getBannerUnit(),
                    adIntervals: 30,
                    style: { left: e, top: t, width: n.width }
                }),
                o = this;
            n.show().then(function() {
                    (o.m_bBannerShowed = !0), o.trackEvent("banner", { step: "display", success: !0 });
                }),
                (o.m_banner = n).onError(function() {
                    o.trackEvent("banner", { step: "display", success: !1 });
                });
        }),
        (a.prototype.hideBanner = function() {
            this.m_bBannerShowed && null != this.m_banner && (this.m_banner.hide(), (this.m_bBannerShowed = !1));
        }),
        (a.prototype.getSubscribeCnt = function() {
            return o(this, void 0, Promise, function() {
                return h(this, function() {
                    return [2, 0];
                });
            });
        }),
        (a.prototype.subscribe = function(e) {
            for (var t = [], n = 1; n < arguments.length; n++) t[n - 1] = arguments[n];
            return o(this, void 0, Promise, function() {
                return h(this, function() {
                    throw new Error("Method not implemented.");
                });
            });
        }),
        (a.prototype.startVideoRecord = function() {}),
        (a.prototype.pauseVideoRecord = function() {}),
        (a.prototype.resumeVideoRecord = function() {}),
        (a.prototype.stopVideoRecord = function() {}),
        (a.prototype.hasVideoRecord = function() {
            return !1;
        }),
        (a.prototype.shareVideoRecord = function() {}),
        (a.prototype.uploadAdEvent = function() {}),
        (a.prototype.uploadAdEvent_Addict = function() {}),
        (a.prototype.addShortcut = function() {}),
        a);

function a() {
    (this.isSharing = !1),
    (this.sdkSwitch = u.DefaultSDKSwitch),
    (this.videoCheckedMap = {}),
    (this.videoCheckedLastT = {}),
    (this.videoMap = {}),
    (this.isLoading = !1),
    (this.isLoggedIn = !1),
    (this.onHideT = 0),
    (this.noAd = !1),
    (this.noVideo = !1),
    (this.lastInterAdShowT = 0),
    (this.switchesInitd = !1),
    (this.ta = null),
    (this.interstitialAd = null),
    (this.interAdReady = !1),
    (this.m_bInitBanner = null),
    (this.m_bBannerShowed = !1),
    (this.m_banner = null);
}
n.WeChatPlatform = e;